#include "001.h"
//extern const char* grade[6];
//HEAD
void HEAD(user*T1)
{
	printf("\033c");
	printf("\n\n");
	printf("\t\t*****************学生信息管理系统***************\n");
	printf("\t\t************************************************\n");
	if(T1->user_reight>0)
	printf("\t\t*****账号：%5s  权限：%14s  ************\n",T1->name,grade[T1->user_reight+2]);	
	else	
	printf("\t\t*****账号：     权限：      ********************\n");	
	printf("\t\t************************************************\n\n");

}
//home
int login_1(user* T1)
{
	char tt1[10];
	while(1)
	{
		HEAD(T1);
		printf("\t\t****按任意键登陆\n");
		printf("\t\t****按q退出\n");
		printf("\t\t");
		scanf(" %s",tt1);
		getchar();
		if(strcmp("q",tt1)==0)return EOF;
		else
		{
			HEAD(T1);
			printf("\t\t****请输入账号：");
			scanf("%s",T1->name);
			getchar();
			printf("\t\t****请输入密码：");
			scanf(" %s",T1->pwssword);
			getchar();
			login_2(1,T1);
			if(T1->user_reight>0&&T1->user_reight<4)return T1->user_reight;
			else if(T1->user_reight==-1)
			{
				HEAD(T1);
				//printf("\t\t账号：%s\n\t\t密码：%s\n\t\t权限：%d\n",T1->name,T1->pwssword,T1->user_reight);
				printf("\t\t****密码错误！！！\n");
				printf("\t\t****密码错误！！！\n");
				printf("\t\t****密码错误！！！\n");
				printf("\t\t****按任意键确认\n");
				printf("\t\t");
				scanf(" %s",tt1);
				getchar();
			}
			else if(T1->user_reight==-2)
			{
				HEAD(T1);
				//printf("\t\t账号：%s\n\t\t密码：%s\n\t\t权限：%d\n",T1->name,T1->pwssword,T1->user_reight);
				printf("\t\t****账号不存在！！！\n");
				printf("\t\t****账号不存在！！！\n");
				printf("\t\t****账号不存在！！！\n");
				printf("\t\t****按任意键确认\n");
				printf("\t\t");
				scanf(" %s",tt1);
				getchar();
			}
		}
	}
}
//用户操作
int login_2(int i,user* T1)
{
	switch(i)
	{
		case 1:
			//查询
			user test={"","",0};
			FILE*p=fopen("./pwssword.txt","a+");
			while(1)
			{
				if(-1==login_3(p)){fclose(p);T1->user_reight=-2;return -2;}
				fscanf(p,"%s",test.name);
				if(-1==login_3(p)){fclose(p);T1->user_reight=-2;return -2;}
				fscanf(p,"%s",test.pwssword);
				if(-1==login_3(p)){fclose(p);T1->user_reight=-2;return -2;}
				fscanf(p,"%d",&test.user_reight);
				if(strcmp(test.name,T1->name)==0)
				{
					if(strcmp(test.pwssword,T1->pwssword)==0)
					{
						T1->user_reight=test.user_reight;
						fclose(p);
						return T1->user_reight;
					}
					else
					{
						T1->user_reight=-1;
						fclose(p);
						return T1->user_reight;
					}
				}
			}
			break;
		case 2:
			//添加
			return 0;
			break;
	}
	return 0;
}
//吸收注释
int login_3(FILE*p)
{
	char t;
	while(1)
	{
		t=fgetc(p);
		if(t==EOF||t==':') break;
	}
	if(t==EOF)
	{
		return -1;
	}
	if(t==':')
	{
		return 0;
	}
	return 0;
}
